<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div id="rightinfo" class="rightinfo"></div>

<!-- 模板 -->
<script id="list" type="text/html">
    <table class="tablelist tabletree">
        <thead>
        <tr>
        <th style="width:8%">编号</th>
        <th>名称</th>
        <th style="width:8%">分组</th>
        <th style="width:8%">模型</th>
        <th style="width:16%">列表地址</th>
        <th style="width:16%">内容地址</th>
        <th style="width:8%">排序</th>
        <th style="width:10%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%if(data[0]){var first = data[0]; for(var i = 0; i < first.length; i ++) {%>
            <tr data-id="<%=first[i].id%>">
            <td><%=first[i].id%></td>
            <td class="tree_first"><span class="toggle">[<%=#category_status[first[i].id]?'+':'-'%>]</span><%=first[i].title%><a class="add_node">添加子分类</a></td>
            <td><%=first[i].gid%></td>
            <td><%=first[i].model%></td>
            <td><%=first[i].list_uri%></td>
            <td><%=first[i].content_uri%></td>
            <td><%=Number(first[i].sort)%></td>
            <td>
                <a class="tablelink act_edit_root">编辑</a>
                <a class="tablelink act_delete_root">删除</a>
            </td>
            </tr>
            <%if(data[first[i].id]){var second = data[first[i].id]; for(var j = 0; j < second.length; j ++) {%>
                <tr data-id="<%=second[j].id%>" class="c_<%=first[i].id%>" <%=#category_status[first[i].id]?'style="display:none"':''%>>
                <td><%=second[j].id%></td>
                <td class="tree_second_<%=(j+1==second.length)?'last':'middle'%>"><%=second[j].title%><a class="add_node">添加子分类</a></td>
                <td><%=second[j].gid%></td>
                <td><%=second[j].model%></td>
                <td><%=second[j].list_uri%></td>
                <td><%=second[j].content_uri%></td>
                <td><%=Number(second[j].sort)%></td>
                <td>
                    <a class="tablelink act_edit">编辑</a>
                    <a class="tablelink act_delete">删除</a>
                </td>
                </tr>
                <%if(data[second[j].id]){var third = data[second[j].id]; for(var k = 0; k < third.length; k ++) {%>
                    <tr data-id="<%=third[k].id%>" class="c_<%=first[i].id%>" <%=#category_status[first[i].id]?'style="display:none"':''%>>
                    <td><%=third[k].id%></td>
                    <td class="tree_third_<%=(j+1==second.length)?'last':'middle'%>_<%=(k+1==third.length)?'last':'middle'%>"><%=third[k].title%></td>
                    <td><%=third[k].gid%></td>
                    <td><%=third[k].model%></td>
                    <td><%=third[k].list_uri%></td>
                    <td><%=third[k].content_uri%></td>
                    <td><%=Number(third[k].sort)%></td>
                    <td>
                        <a class="tablelink act_edit">编辑</a>
                        <a class="tablelink act_delete">删除</a>
                    </td>
                    </tr>
                <%}}%>
            <%}}%>    
        <%}}%>        
        </tbody>
    </table>
    <div class="clearfloat"></div>
</script>

<script id="edit" type="text/html">
<form action="<?php echo site_url('ajax/category_handle/modify_subnode');?>" method="POST">  
<div class="formtitle"><span>编辑子分类</span></div> 
<input name="id" type="hidden" value="<%=id?id:0%>" />
<ul class="forminfo">
    <li>
        <label>上级分类</label>
        <select name="fid" class="selectmenu">
        <%if(tree[0]){var first = tree[0]; for(var i = 0; i < first.length; i ++) {%>
            <option value="<%=first[i].id%>" <%=first[i].id==fid?'selected="selected"':''%>><%=first[i].title%></option>
            <%if(tree[first[i].id]){var second = tree[first[i].id]; for(var j = 0; j < second.length; j ++) {%>
                <%if(second[j].id != id){%>
                <option value="<%=second[j].id%>" <%=second[j].id==fid?'selected="selected"':''%>>&nbsp;&nbsp;|--&nbsp;<%=second[j].title%></option>
                <%}%>
            <%}}%>
        <%}}%>
        </select>
    </li>
    <li><label>分类名称</label><input name="title" value="<%=title%>" type="text" class="dfinput" /></li>
    <li><label>英文名称</label><input name="subtitle" value="<%=subtitle%>" type="text" class="dfinput" /></li>
    <li><label>排序</label><input name="sort" value="<%=Number(sort)%>" type="text" class="dfinput" /><i>数值大的在前，数值小的在后</i></li>
    <li><label>&nbsp;</label><input type="submit" class="btn" value="确认保存"/><input type="button" class="btn btn_cancle" value="取消"/></li>
</ul>
</form>
</script>

<script id="edit_root" type="text/html">
<form action="<?php echo site_url('ajax/category_handle/modify_root');?>" method="POST">  
<div class="formtitle"><span><%=id?'编辑':'添加'%>根分类</span></div> 
<input name="id" type="hidden" value="<%=id?id:0%>" />
<ul class="forminfo">
    <li><label>分类名称<b>*</b></label><input name="title" value="<%=title%>" type="text" class="dfinput" /></li>
    <li><label>英文名称</label><input name="subtitle" value="<%=subtitle%>" type="text" class="dfinput" /></li>
    <li><label>分组</label><input name="gid" value="<%=Number(gid)%>" type="text" class="dfinput" /><i>请勿与已有分组相同</i></li>
    <li><label>模型</label><input name="model" value="<%=Number(model)%>" type="text" class="dfinput" /><i>模型代号：0信息，1新闻，2产品，3下载</i></li>
    <li><label>列表地址</label><input name="list_uri" value="<%=list_uri%>" type="text" class="dfinput" /><i>例如：home/xxxx，后面没有.html，如果模型没有列表页，如信息，请留空</i></li>
    <li><label>内容地址<b>*</b></label><input name="content_uri" value="<%=content_uri%>" type="text" class="dfinput" /><i>例如：home/xxxx，后面没有.html</i></li>
    <li><label>列表模板</label><input name="list_tpl" value="<%=list_tpl%>" type="text" class="dfinput" /><i>需在模板目录下有对应的模板文件，使用模型对应的默认模板请留空</i></li>
    <li><label>内容模板</label><input name="content_tpl" value="<%=content_tpl%>" type="text" class="dfinput" /><i>需在模板目录下有对应的模板文件，使用模型对应的默认模板请留空</i></li>
    <li><label>&nbsp;</label><input type="submit" class="btn" value="确认保存"/><input type="button" class="btn btn_cancle" value="取消"/></li>
</ul>
</form>
</script>

<!-- 脚本 -->
<script src="../plugins/jquery.cookie.js" type="text/javascript"></script>
<script src="js/template-helper.js" type="text/javascript"></script>
<script type="text/javascript">
layer.config({extend: 'extend/layer.ext.js'});
function get_page(){
    $.getJSON(
        '<?php echo site_url('ajax/category_handle/get_tree_super');?>',
        {},
        function(rs){
            if(rs.err===0){
            	rs.category_status = {};
            	if($.cookie('CATEGORY_STATUS')){
            		$.extend(rs.category_status, JSON.parse($.cookie('CATEGORY_STATUS')));
            	}
            	$('#rightinfo').html(template('list', rs));                
            }else{
                layer.msg(rs.msg, {icon:2,time:2000});
            }            
        }
    );
};
//初始化
function init(){
    var hash = window.location.hash.split('/');
    if(hash[0]=='#add'){
    	$('.btn_add').hide();
    	$('.btn_back').show();
    	$.getJSON(
                '<?php echo site_url('ajax/category_handle/prepare_add_root');?>',
                {},
                function(rs){
                    if(rs.err===0){
                    	$('#rightinfo').html(template('edit_root', $.extend({gid:0,model:0},rs.data)));
                    	$( ".selectmenu" ).selectmenu({width: 162});  
                    }else{
                          layer.msg(rs.msg, {icon:2,time:2000});
                    }
                }
        );
    }else if(hash[0]=='#edit' && hash[1]>0){
        var id = hash[1];
    	$('.btn_add').hide();
    	$('.btn_back').show();
    	$.getJSON(
                '<?php echo site_url('ajax/category_handle/get_subnode');?>',
                {
                    id:id
                },
                function(rs){
                    if(rs.err===0){                    	
                    	$('#rightinfo').html(template('edit', rs.data));
                    	$( ".selectmenu" ).selectmenu({width: 162});
                    }else{
                        layer.msg(rs.msg, {icon:2,time:2000});
                        window.location.hash = '';
                    	init();
                    }
                }
        );   	
    }else if(hash[0]=='#edit_root' && hash[1]>0){
        var id = hash[1];
    	$('.btn_add').hide();
    	$('.btn_back').show();
    	$.getJSON(
                '<?php echo site_url('ajax/category_handle/get_root');?>',
                {
                    id:id
                },
                function(rs){
                    if(rs.err===0){                    	
                    	$('#rightinfo').html(template('edit_root', rs.data));
                    }else{
                        layer.msg(rs.msg, {icon:2,time:2000});
                        window.location.hash = '';
                    	init();
                    }
                }
        );   	
    }else{
    	$('.btn_add').show();
    	$('.btn_back').hide();
    	get_page();	
    }
}
init();

//删除
$('#rightinfo').on('click', '.act_delete',function(){
    var id = $(this).parent().parent().attr('data-id');
    layer.confirm('此操作将一并删除该分类下的所有子分类，以及属于这些分类的所有内容，确定删除？', {icon: 3, title:'提示', btn: ['删除','取消']}, function(){
        $.post(
                '<?php echo site_url('ajax/category_handle/delete_subnode');?>',
                {
                      id:id
                },
                function(rs){
                    if(rs.err===0){
                        layer.msg('删除成功！', {icon:1,time:2000});    
                        get_page();
                    }else{
                        layer.msg(rs.msg, {icon:2,time:2000});
                    }
                },
                'json'
        );
    });
});
$('#rightinfo').on('click', '.act_delete_root',function(){
    var id = $(this).parent().parent().attr('data-id');
    layer.confirm('此操作将一并删除该分类下的所有子分类，以及属于这些分类的所有内容，确定删除？', {icon: 3, title:'提示', btn: ['删除','取消']}, function(){
        $.post(
                '<?php echo site_url('ajax/category_handle/modify_root');?>',
                {
                    id:id,
                    deleted:1
                },
                function(rs){
                    if(rs.err===0){
                        layer.msg('删除成功！', {icon:1,time:2000});    
                        get_page();
                    }else{
                        layer.msg(rs.msg, {icon:2,time:2000});
                    }
                },
                'json'
        );
    });
});
//编辑
$('#rightinfo').on('click', '.act_edit', function(){
	var id = $(this).parent().parent().attr('data-id');    
	window.location.hash = 'edit/'+id;
	init();
});
$('#rightinfo').on('click', '.act_edit_root', function(){
	var id = $(this).parent().parent().attr('data-id');    
	window.location.hash = 'edit_root/'+id;
	init();
});
//保存
$('#rightinfo').on('submit','form', function(e) {
    e.preventDefault();
	var this_btn = $(this);
    if(!this_btn.attr('disabled')){
    	this_btn.attr('disabled',true);
    }else{
        return;
    }
    $(this).ajaxSubmit({
    	dataType:'json',
    	success:function(rs){
    		if(rs.err===0){
                layer.msg('保存成功！', {icon:1,time:2000}, function(){
        			window.location.hash = '';
        			init();
                });
            }else{
                layer.msg(rs.msg, {icon:2,time:2000}, function(){
                	this_btn.attr('disabled',false);
                });
            }
		}
    });
});
//添加
$('.btn_add,.news_add').on('click', function(){
	window.location.hash = 'add';
	init();
});
//返回列表
$('.btn_back').on('click', function(){
	window.location.hash = '';
	init();
});
//取消
$('#rightinfo').on('click', '.btn_cancle', function(){
	window.location.hash = '';
	init();
});
//添加子分类
$('#rightinfo').on('click', '.add_node', function(){
	var id = $(this).parent().parent().attr('data-id');    
	layer.prompt({title: '输入分类名称', formType: 0}, function(title){
		$.post(
	            '<?php echo site_url('ajax/category_handle/add_subnode');?>',
	            {
	            	fid:id,
	            	title:title
	            },
	            function(rs){
	            	if(rs.err===0){
	            		layer.msg('添加成功！', {icon:1,time:2000});
	            		get_page();          		
	            	}else{
	            		layer.msg(rs.msg, {icon:2});
	            	}
	            },
	            'json'
	    );
	});
});
//分类展开、折叠
$('#rightinfo').on('click', '.toggle', function(){
	var id = $(this).parent().parent().attr('data-id');
	var category_status = {};
	if($.cookie('CATEGORY_STATUS')){
		$.extend(category_status,JSON.parse($.cookie('CATEGORY_STATUS')));
	}
	if($(this).text()=='[-]'){
		category_status[id] = true;
		$(this).text('[+]');
		$('.c_'+id).hide();
	}else{
		$(this).text('[-]');
		category_status[id] = false;
		$('.c_'+id).show();
	}
	$.cookie('CATEGORY_STATUS',JSON.stringify(category_status),{ path: '/' });
});
</script>